package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IBannerService;
import com.mdd.admin.validate.banner.BannerCreateValidate;
import com.mdd.admin.validate.banner.BannerSearchValidate;
import com.mdd.admin.validate.banner.BannerUpdateValidate;
import com.mdd.admin.vo.banner.BannerDetailVo;
import com.mdd.admin.vo.banner.BannerListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.banner.Banner;
import com.mdd.common.mapper.banner.BannerMapper;
import com.mdd.common.util.StringUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 广告服务实现类
 */
@Service
public class BannerServiceImpl implements IBannerService {

    @Resource
    BannerMapper bannerMapper;


    /**
     * 广告列表
     *
     * @param pageValidate   分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<BannerListVo>
     * @author shh
     */
    @Override
    public PageResult<BannerListedVo> list(PageValidate pageValidate, BannerSearchValidate searchValidate) {
        Integer pageNo = pageValidate.getPageNo();
        Integer pageSize = pageValidate.getPageSize();

        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(searchValidate.getStartTime())) {
            queryWrapper.ge("create_time", searchValidate.getStartTime());
        }
        if (StringUtils.isNotEmpty(searchValidate.getStartTime())) {
            queryWrapper.le("create_time", searchValidate.getEndTime());
        }
        if (StringUtils.isNotEmpty(searchValidate.getTitle())) {
            queryWrapper.like("title", searchValidate.getTitle());
        }
        if (StringUtils.isNotEmpty(searchValidate.getContent())) {
            queryWrapper.like("content", searchValidate.getContent());
        }
        if (searchValidate.getBannerType() != null) {
            queryWrapper.eq("banner_type", searchValidate.getBannerType());
        }
        queryWrapper.eq("is_show", searchValidate.getIsShow() == null ? 0 : searchValidate.getIsShow());
        queryWrapper.orderByDesc("id");

        IPage<Banner> iPage = bannerMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);

        List<BannerListedVo> list = new LinkedList<>();
        for (Banner item : iPage.getRecords()) {
            BannerListedVo vo = new BannerListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setImgSrc(UrlUtils.toAbsoluteUrl(vo.getImgSrc()));
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 广告详情
     *
     * @param id 主键ID
     * @author shh
     */
    @Override
    public BannerDetailVo detail(Long id) {
        Banner model = bannerMapper.selectOne(new QueryWrapper<Banner>().eq("id", id));
        Assert.notNull(model, "广告不存在");
        BannerDetailVo vo = new BannerDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 广告新增
     *
     * @param createValidate 广告参数
     * @author shh
     */
    @Transactional
    @Override
    public void add(BannerCreateValidate createValidate) {
        Banner model = new Banner();
        model.setTitle(createValidate.getTitle());
        model.setContent(createValidate.getContent());
        model.setImgSrc(UrlUtils.toRelativeUrl(createValidate.getImgSrc()));
        model.setLink(createValidate.getLink());
        model.setBannerType(createValidate.getBannerType());
        model.setIsShow(createValidate.getIsShow());
        bannerMapper.insert(model);
    }

    /**
     * 广告编辑
     *
     * @param updateValidate 广告参数
     * @author shh
     */
    @Transactional
    @Override
    public void edit(BannerUpdateValidate updateValidate) {
        Banner model = bannerMapper.selectOne(
                new QueryWrapper<Banner>()
                        .eq("id", updateValidate.getId()));

        Assert.notNull(model, "广告不存在!");
        model.setId(updateValidate.getId());
        model.setTitle(updateValidate.getTitle());
        model.setImgSrc(UrlUtils.toRelativeUrl(updateValidate.getImgSrc()));
        model.setLink(updateValidate.getLink());
        model.setContent(updateValidate.getContent());
        model.setBannerType(updateValidate.getBannerType());
        model.setIsShow(updateValidate.getIsShow());
        bannerMapper.updateById(model);
    }

    /**
     * 广告删除
     *
     * @param id 广告ID
     * @author shh
     */
    @Transactional
    @Override
    public void del(Long id) {
        Banner banner = bannerMapper.selectOne(
                new QueryWrapper<Banner>()
                        .select("id,is_show")
                        .eq("id", id));

        Assert.notNull(banner, "广告不存在!");
        bannerMapper.deleteById(banner);
    }

}
